package apis

import (
	"fmt"

	"commonUtils/utils"
)

type Api struct {
	name  string
	param map[string]interface{}
}

func NewApi(name string, param map[string]interface{}) *Api {
	if nil == param {
		param = map[string]interface{}{}
	}
	return &Api{
		name:  name,
		param: param,
	}
}

func (a *Api) SetParam(k string, v interface{}) {
	a.param[k] = v
}
func (t *Api) Param() map[string]interface{} {
	return t.param
}

func (a *Api) Name() string {
	return a.name
}

func (t *Api) CheckParam(obj interface{}) error {
	validator := utils.GetValidate()
	err := validator.Struct(obj)
	if err != nil {
		return err
	}

	json, err := utils.ToJson(obj)

	if err != nil {
		return fmt.Errorf("ToJson error : %s", err)
	}

	mdata, err := utils.JsonToMap(json)

	if err != nil {
		return fmt.Errorf("JsonToMap error : %s", err)
	}

	for k, v := range mdata {
		t.param[k] = v
	}

	return nil
}
